let utils = {}

utils.login = () => {
  return new Promise((resolve, reject) => {
    uni.login({
      provider: 'weixin',
      success (res) {
        if (res.code) {
          resolve(res.code)
        } else {
          reject('登录失败')
        }
      },
      fail (e) {
        reject(e.toString())
      }
    })
  })
}

utils.getLocation = () => {
  return new Promise((resolve, reject) => {
    uni.getLocation({
      type: 'wgs84',
      success (res) {
        resolve(res)
      },
      fail (e) {
        reject(e.toString())
      }
    })
  })
}

utils.getUserInfo = () => {
  return new Promise((resolve, reject) => {
    uni.getUserInfo({
      provider: 'weixin',
      withCredentials: true,
      success (res) {
        console.log('userInfo', res)
        resolve(res)
      },
      fail (e) {
        reject(e)
      }
    })
  })
}

utils.getAuthSetting = authString => {
  return new Promise((resolve, reject) => {
    uni.getSetting({
      success (res) {
        //scope.userInfo
        if (res.authSetting[authString]) {
          resolve(true)
        } else {
          resolve(false)
        }
      },
      fail (e) {
        reject(e)
      }
    })
  })
}

utils.requestPayment = (timeStamp, nonceStr, pkg, paySign) => {
  return new Promise((resolve, reject) => {
    uni.requestPayment({
      provider: "wxpay",
      orderInfo: "orderInfo",
      timeStamp: timeStamp,
      nonceStr: nonceStr,
      package: pkg,
      paySign: paySign,
      signType: 'MD5',
      success: function (res) {
        console.log('支付成功', res)
        resolve(true)
      },
      fail: function (res) {
        console.log(res)
        reject(false)
      }
    })
  })
}

utils.setNavigationBarTitle = async (title) => {
  try {
    await uni.setNavigationBarTitle({
      title,
    });
  } catch (e) {
    console.log(e)
  }
}

utils.setStorage = async (key, value) => {
  try {
    await uni.setStorageSync(key, value)
  } catch (e) {
    console.log(e)
  }
}

utils.getStorage = async key => {
  return new Promise((resolve, reject) => {
    try {
      uni.getStorage({
        key: key,
        success (res) {
          resolve(res.data)
        },
        fail (e) {
          resolve(undefined)
        }
      })
    } catch (e) {
      console.log(e)
      reject(e)
    }
  })
}

utils.navigateTo = async url => {
  uni.navigateTo({
    url
  })
}

utils.navigateBack = async delta => {
  uni.navigateBack({
    delta
  })
}

utils.switchTab = async url => {
  uni.switchTab({
    url
  })
}

utils.redirectTo = async url => {
  uni.redirectTo({
    url
  })
}

utils.reLaunch = async url => {
  uni.reLaunch({
    url
  })
}

utils.chooseImage = (count, sizeType, sourceType) => {
  return new Promise((resolve, reject) => {
    try {
      uni.chooseImage({
        count,
        sizeType,
        sourceType,
        success (res) {
          console.log('success', res)
          resolve(res)
        },
        fail (e) {
          console.log(e)
          resolve(undefined)
        }
      })
    } catch (e) {
      console.log(e)
      reject(e)
    }
  })
}

utils.showLoading = (title = '加载中') => {
  uni.showLoading({
    title,
    mask: true,
  })
}

utils.hideLoading = () => {
  uni.hideLoading()
}

utils.showToast = (msg, icon = 'none') => {
  uni.showToast({
    title: msg,
    icon,
    duration: 2000
  })
}

utils.makePhoneCall = (phoneNumber) => {
  uni.makePhoneCall({ phoneNumber });
}

utils.chooseAddress = () => {
  return new Promise((resolve, reject) => {
    try {
      uni.chooseAddress({
        success (res) {
          console.log('success', res)
          resolve(res)
        },
        fail (e) {
          console.log('fail', e)
          resolve(undefined)
        }
      })
    } catch (e) {
      console.log('catch', e)
      reject(e)
    }
  })
}

utils.scanCode = (scanType) => {
  return new Promise((resolve, reject) => {
    try {
      uni.scanCode({
        scanType,
        success (res) {
          console.log('success', res)
          resolve(res)
        },
        fail (e) {
          console.log('fail', e)
          resolve(undefined)
        }
      })
    } catch (e) {
      console.log('catch', e)
      reject(e)
    }
  })
}

utils.UploadFile = file => {
  return new Promise((resolve, reject) => {
    uni.uploadFile({
      url: 'https://file.touchdot.top/uploadSingleFile',
      filePath: file,
      name: 'file',
      success: function (res) {
        resolve(res)
      },
      fail: function (res) {
        reject(res)
      }
    })
  })
}

utils.ChooseImage = (count, sizeType, sourceType) => {
  return new Promise((resolve, reject) => {
    try {
      uni.chooseImage({
        count,
        sizeType,
        sourceType,
        success (res) {
          console.log('success', res)
          resolve(res)
        },
        fail (err) {
          console.log('fail', err)
          resolve(undefined)
        }
      })
    } catch (err) {
      console.log('catch', err)
      reject(err)
    }
  })
}

utils.PreviewImage = (urls, current) => {
  return new Promise((resolve, reject) => {
    uni.previewImage({
      urls,
      current,
      success (res) {
        resolve(res)
      },
      fail (res) {
        reject(res)
      }
    })
  })
}

utils.setClipboardData = (val) => {
  return new Promise((resolve, reject) => {
    uni.setClipboardData({
      data: val,
      success (res) {
        resolve(res)
      },
      fail (res) {
        reject(res)
      }
    });
  })
}

utils.SetNavigationBarTitle = title => {
  uni.setNavigationBarTitle({
    title
  })
}

export default utils
